/**
 * created by 小卷毛, 2023-05-10
 * Copyright (c) 2023, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.google.android.material.tabs.TabLayout
import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.R
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.model.FilterData
import com.rzico.sbl.model.ManagerContent
import com.rzico.sbl.model.ManagerDivider
import com.rzico.sbl.model.ResponseModel
import com.rzico.sbl.model.StockBalance
import com.rzico.sbl.model.StockBarrel
import com.rzico.sbl.model.StockGoods
import com.rzico.sbl.model.StockRecord
import com.rzico.sbl.model.StockShop
import com.rzico.sbl.model.StockSupply
import com.rzico.sbl.model.StoreData
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.find
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.applySchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.common_ui.view.BadgeTextView
import io.reactivex.Single

/**
 * 描 述：进销存统计相关
 * 作 者：小卷毛  2023-05-10 13:59
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class StatisticStockViewModel : DialogViewModel() {

    fun stockIcons() = arrayListOf(
        ManagerDivider("商品"),
        ManagerContent("商品库存", R.mipmap.icon_stock02, "1"),
        ManagerContent("账面结存", R.mipmap.icon_stock05, "2"),
        ManagerContent("库存盘点", R.mipmap.icon_stock06, "3"),
        ManagerContent(),
        ManagerDivider("采购"),
        ManagerContent("供应商", R.mipmap.icon_stock07, "4"),
        ManagerContent("采购入库", R.mipmap.icon_stock03, "5"),
        ManagerContent(),
        ManagerContent(),
        ManagerDivider("空桶"),
        ManagerContent("空桶库存", R.mipmap.icon_stock08, "6"),
        ManagerContent("账面结存", R.mipmap.icon_stock09, "7"),
        ManagerContent("库存盘点", R.mipmap.icon_stock10, "8"),
        ManagerContent(),
        ManagerDivider()
    )

    val timeList by lazy {
        listOf(
            FilterData("全部", "0", "时间", true),
            FilterData("今日", "1", "今日"),
            FilterData("昨日", "2", "昨日"),
            FilterData("本周", "3", "本周"),
            FilterData("本月", "4", "本月"),
            FilterData("上月", "5", "上月"),
            FilterData("本年", "6", "本年"),
            FilterData("自定义时间", "7", "自定义")
        )
    }

    fun reasonIn() = arrayListOf(
        "手动入库",
        "进货入库",
        "退货",
        "特殊情况入库",
    )

    fun reasonOut() = arrayListOf("手动出库")

    /**
     * 仓库列表
     */
    fun shopList(
        onFinally: () -> Unit = {},
        onSuccess: (ArrayList<StockShop>) -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ArrayList<StockShop>>>(BaseUrl.psiShop)
            .converter(object : JsonConvert<BaseResponse<ArrayList<StockShop>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 仓库商品库存列表
     */
    fun goodsList(
        page: Int,
        keyWord: String,
        shopId: String,
        isLoading: Boolean = false,
        onSuccess: (ResponseModel<StockGoods>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<StockGoods>>>(BaseUrl.psiProduct)
            .isMultipart(true)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "shopId" to shopId,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockGoods>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .doOnSubscribe { (isLoading && page == 0).condition { showLoading() } }
            .doFinally { (isLoading && page == 0).condition { dismissLoading() } }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 仓库商品库存明细列表
     */
    fun goodsDetail(
        page: Int,
        stockId: String,
        onSuccess: (ArrayList<StockRecord>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<StockRecord>>>(BaseUrl.psiStockDet)
            .params(
                getParams(
                    "id" to stockId,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockRecord>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 商品结存
     */
    fun goodsbalance(
        page: Int,
        flag: String = "0",
        shopId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ResponseModel<StockBalance>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<StockBalance>>>(BaseUrl.psiStockBal)
            .params(
                getParams(
                    "flag" to flag,
                    "shopId" to shopId,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockBalance>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 供应商列表
     */
    fun supplyList(
        page: Int,
        pageSize: Int = 10,
        isLoading: Boolean = false,
        onSuccess: (ResponseModel<StockSupply>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<StockSupply>>>(BaseUrl.supList)
            .params(
                getParams(
                    "pageStart" to page * pageSize,
                    "pageSize" to pageSize
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockSupply>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .doOnSubscribe { isLoading.condition { showLoading() } }
            .doFinally { isLoading.condition { dismissLoading() } }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 供应商欠款、欠桶明细列表
     */
    fun supplyRecord(
        page: Int,
        type: String,
        supplyId: String,
        onSuccess: (ArrayList<StockRecord>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<StockRecord>>>(BaseUrl.supLog)
            .params(
                getParams(
                    "id" to supplyId,
                    "type" to type,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockRecord>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 采购入库列表
     */
    fun storeList(
        page: Int,
        status: String,
        shopId: String,
        supplyId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (ArrayList<StoreData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseModel<StoreData>>>(BaseUrl.psiStoreLis)
            .params(
                getParams(
                    "status" to status,
                    "shopId" to shopId,
                    "supplierId" to supplyId,
                    "beginDate" to beginDate,
                    "endDate" to endDate,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StoreData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 采购入库角标
     */
    fun orderCount(): Single<String> =
        RxSingleHelper
            .requestString(
                url = BaseUrl.psiStoreCou
            )
            .compose(applySchedulers())

    /**
     * 更新自定义Tab数量
     */
    fun updateNum(
        debit: String,
        layout: TabLayout
    ) {
        layout.getTabAt(1)?.let { tab ->
            tab.customView
                ?.find<BadgeTextView>(R.id.item_tab_num)
                ?.setBadgeCount(debit)
        }
    }

    /**
     * 仓库空桶库存列表
     */
    fun barrelList(
        page: Int,
        keyWord: String,
        shopId: String,
        isLoading: Boolean = false,
        onSuccess: (ResponseModel<StockBarrel>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<StockBarrel>>>(BaseUrl.psiBarrel)
            .isMultipart(true)
            .params(
                getParams(
                    "searchValue" to keyWord,
                    "shopId" to shopId,
                    "pageStart" to page * 10,
                    "pageSize" to 10
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<StockBarrel>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .doOnSubscribe { (isLoading && page == 0).condition { showLoading() } }
            .doFinally { (isLoading && page == 0).condition { dismissLoading() } }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}